package com.yuncool.auth.config;

import com.yuncool.auth.handler.*;
import com.yuncool.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.web.filter.CharacterEncodingFilter;


/**
 * Spring Security 配置类
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    UserService userService;
    @Autowired
    AuthSuccessHandler authSuccessHandler;
    @Autowired
    AuthFailureHandler authFailureHandler;
    @Autowired
    AuthAccessDeniedHandler authAccessDeniedHandler;
    @Autowired
    AuthLogoutSuccessHandler authLogoutSuccessHandler;
    @Autowired
    LoginAuthenticationEntryPoint loginAuthenticationEntryPoint;


    /**
     * 线上部署请注释此代码
     * 配置不拦截url
     *
     * @param web
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/web/**", "/v2/api-docs/**", "/swagger-resources/**", "/webjars/**", "/doc.html");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/index/**", "/common/**", "/auth/**", "/test/**", "/css/**", "/image/**", "/fonts/**", "/js/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/auth/login")
                .successHandler(authSuccessHandler)
                .failureHandler(authFailureHandler)
                .and()
                .exceptionHandling().authenticationEntryPoint(loginAuthenticationEntryPoint)
                .and()
                .exceptionHandling().accessDeniedHandler(authAccessDeniedHandler)
                .and()
                .logout()
                .logoutUrl("/auth/logout")
                .logoutSuccessHandler(authLogoutSuccessHandler)
                .and()
                .csrf().disable();
        //限制单一用户
        http.sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true);
        //退出清楚Cookie
        http.logout().deleteCookies("JESSIONID");
        //解决乱码
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);
        http.addFilterBefore(filter, CsrfFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
